package com.eastfair.venueservice.service.impl;

import com.alibaba.nacos.common.utils.Objects;
//import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eastfair.annotation.annotation.cache.Cache;
import com.eastfair.annotation.annotation.cache.Magic;
import com.eastfair.boot.service.SuperServiceImpl;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.venueservice.dao.WorkOrderOperationRecordMapper;
import com.eastfair.venueservice.dto.WorkOrderOperationRecordDTO;
import com.eastfair.venueservice.entity.WorkOrderOperationRecord;
import com.eastfair.venueservice.service.WorkOrderOperationRecordService;
import com.eastfair.venueservice.vo.WorkOrderOperationRecordVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* <p>
    * 业务实现类
    * 工单操作记录
    * </p>
*
* @author linan
* @date 2022-10-14
*/
@Slf4j
@Service
//@DS("#thread.tenant")
    public class WorkOrderOperationRecordServiceImpl extends SuperServiceImpl<WorkOrderOperationRecordMapper, WorkOrderOperationRecord> implements WorkOrderOperationRecordService {


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
    */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<WorkOrderOperationRecord> modelList) {
    modelList.stream().forEach(s -> {
    s.setIsDeleted(BusinessConstant.YES);
    });
    // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
    return R.successDef();
    }

    @Override
    protected R<Boolean> handlerSave(WorkOrderOperationRecord model) {
        model.setProjectId(ContextUtil.getProjectId());
        model.setSubsystemId(ContextUtil.getSubSystemId());
        return R.successDef();
    }

    /**
    * 通用查询
    *
    * @param modelDTO DTO
    * @param ids 主键id集合
    * @return list
    */
    @Override
    public List<WorkOrderOperationRecord> list(WorkOrderOperationRecordDTO modelDTO, List<Long> ids) {
        return ((WorkOrderOperationRecordService) AopContext.currentProxy()).listOfCache(modelDTO, ids);
    }


    /**
     * 缓存查询
     *
     * @param modelDTO DTO
     * @param ids 主键id集合
     * @return list
     */
    @Override
    @Cache(key = "",
       magic = @Magic(
       key = "{"+
       "{'isEnabled', #args[0]?.isEnabled},"+
       "{'workOrderNo', #args[0]?.workOrderNo},"+
       "{'operationType', #args[0]?.operationType},"+
       "{'userId', #args[0]?.userId},"+
       "{'userName', #args[0]?.userName},"+
       "{'orgId', #args[0]?.orgId},"+
       "{'orgName', #args[0]?.orgName},"+
       "{'fileUrl', #args[0]?.fileUrl},"+
       "{'lastLinkFinishTime', #args[0]?.lastLinkFinishTime},"+
       "{'remark', #args[0]?.remark},"+
        "{'id', #args[1]}"+
        "}"))
    public List<WorkOrderOperationRecord> listOfCache(WorkOrderOperationRecordDTO modelDTO, List<Long> ids) {
       //查询
       QueryWrapper<WorkOrderOperationRecord> queryWrapper = new QueryWrapper<>();
       if (modelDTO != null) {
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getWorkOrderNo()),WorkOrderOperationRecord::getWorkOrderNo,modelDTO.getWorkOrderNo());
                    queryWrapper.lambda().eq(Objects.nonNull(modelDTO.getOperationType()),WorkOrderOperationRecord::getOperationType,modelDTO.getOperationType());
                    queryWrapper.lambda().eq(modelDTO.getUserId() != null,WorkOrderOperationRecord::getUserId,modelDTO.getUserId());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getUserName()),WorkOrderOperationRecord::getUserName,modelDTO.getUserName());
                    queryWrapper.lambda().eq(modelDTO.getOrgId() != null,WorkOrderOperationRecord::getOrgId,modelDTO.getOrgId());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getOrgName()),WorkOrderOperationRecord::getOrgName,modelDTO.getOrgName());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getFileUrl()),WorkOrderOperationRecord::getFileUrl,modelDTO.getFileUrl());
                    queryWrapper.lambda().eq(modelDTO.getLastLinkFinishTime() != null,WorkOrderOperationRecord::getLastLinkFinishTime,modelDTO.getLastLinkFinishTime());
                    queryWrapper.lambda().eq(StringUtils.isNotBlank(modelDTO.getRemark()),WorkOrderOperationRecord::getRemark,modelDTO.getRemark());
            }
            if (ids != null && !ids.isEmpty()) {
            queryWrapper.lambda().in(WorkOrderOperationRecord::getId, ids);
            }
            queryWrapper.lambda().eq(WorkOrderOperationRecord::getIsDeleted, BusinessConstant.DELETE_NO);
            List<WorkOrderOperationRecord> modelList = list(queryWrapper);
            return modelList;
    }


    /**
     * do转vo
     * @param model model
     * @return modelVo
     */
    @Override
    public WorkOrderOperationRecordVO doToVo(WorkOrderOperationRecord model) {
        WorkOrderOperationRecordVO modelVo = new WorkOrderOperationRecordVO();
        BeanUtils.copyProperties(model, modelVo);
        modelVo.setId(model.getId());
        return modelVo;
    }
    /**
     * do转vo
     * @param modelList modelList
     * @return list
     */
     @Override
     public List<WorkOrderOperationRecordVO> doToVos(List<WorkOrderOperationRecord> modelList) {
        if (modelList == null || modelList.isEmpty()) {
        return null;
        }
        List<WorkOrderOperationRecordVO> result = new ArrayList<>();
        for (WorkOrderOperationRecord model : modelList) {
           result.add(doToVo(model));
        }
        return result;
    }
}
